package com.wnhoo.smartfishtank.api;

import android.util.Log;

import com.google.gson.Gson;
import com.wnhoo.smartfishtank.api.data.LM_AccessToken;
import com.wnhoo.smartfishtank.api.data.LM_Register;
import com.wnhoo.smartfishtank.api.other.LM_ContantsPostKeyAPI;
import com.wnhoo.smartfishtank.api.other.MD5Util;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kevinwen on 2014/12/26.
 * Copyright (c) 2014/12/26.KevinWen. All rights reserved.
 */
public class LM_User {

    /**
     *
     * http://api.lemoniot.com/user/register?username=**&password=**&timestamp=**
     * http://api.lemoniot.com/user/changepasswd?token=**&password=**&oldpassword=**&timestamp=**
     * http://api.lemoniot.com/user/login?username=**&password=**&timestamp=**
     */

    private final static String base_url = LM_BasicAPI.BASE_URL + "user/";


    public static LM_Register register(boolean isDebug,String username,String password)throws ClientProtocolException, IOException {

        List<NameValuePair> parameterList = new ArrayList<NameValuePair>();
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_USERNAME,username ));
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_PASSWORD,MD5(password )));
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_TIMESTAMP, "x"));

        String url = base_url + "register";
        String str = LM_BasicAPI.baseAPI(isDebug,url , parameterList);

        Gson gson = new Gson();
        return gson.fromJson(str, LM_Register.class);
    }

    public static LM_AccessToken login(boolean isDebug,String username,String password,String language)throws ClientProtocolException, IOException {

        List<NameValuePair> parameterList = new ArrayList<NameValuePair>();
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_USERNAME,username ));
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_PASSWORD,MD5(password) ));
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_TIMESTAMP,language));

        String url = base_url + "login";
        String str = LM_BasicAPI.baseAPI(isDebug,url , parameterList);

        Log.i("LM_User", "尝试用mac地址登陆" + str);
        Gson gson = new Gson();
        return gson.fromJson(str, LM_AccessToken.class);
    }
    public static void changePassWord(boolean isDebug,String token,String old_password,String password)throws ClientProtocolException, IOException {
        List<NameValuePair> parameterList = new ArrayList<NameValuePair>();
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_ACCESS_TOKEN,token ));
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_OLD_PASSWORD,MD5(old_password )));
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_PASSWORD,MD5(password )));
        parameterList.add(new BasicNameValuePair(LM_ContantsPostKeyAPI.PARAMETER_TIMESTAMP, "x"));

        //token=**&password=**&oldpassword=**&timestamp=**
        String url = base_url + "changepasswd";
        String str = LM_BasicAPI.baseAPI(isDebug,url , parameterList);

    }


    private static String MD5(String data){

        MD5Util getMD5 = new MD5Util();
        return getMD5.md5Signature(data);
    }

}
